﻿(function () {
    "use strict";
    var DownTimer = WinJS.Class.define(function () {
        this.time = 0;
        this.length = 0;
    }, {
        time:0,
        length: 0,
        //gets the time passed since the start was pushed as a DateTime value
        _since: function () { return DownTimer._now() - this.time; },
        hours: function () {
            return Math.floor( this.currentTimeMillis() / 3600000 ) % 60;
        },
        minutes: function () {
            return Math.floor(this.currentTimeMillis() / 60000) % 60;
        },
        seconds: function () {
            return Math.floor(this.currentTimeMillis() / 1000) % 60;
        },
        milliseconds: function () {
            return this.currentTimeMillis() % 1000;
        },
        isRunning: function () {
            return (this.length == 0 && this.time > 0) ||
                (this.length > 0 && this.time > 0 && (this.length - this._since()) > 0);
        },
        //starts the timer
        start: function (length) { this.time = DownTimer._now(); this.length = length * 1000; },
        //stops the timer
        stop: function () {
            this.time = 0;
            //this.length = 0;
        },
        
        currentTimeMillis: function(){
            var l=0;
            if (this.length == 0) {
                if (this.time != 0)
                    l = this._since();
            }
            else {
                if (this.time != 0) {
                    l = this.length - this._since();
                    if (l < 0)
                        this.stop(); //reset time when reached 0
                }
                else
                    l = this.length;
            }
            return l > 0 ? l : 0;
        },
        //gets the time lasted as a time formatted string (00:00:00);
        toString: function () {
            return DownTimer.MillisecondsToTimeString(this.currentTimeMillis());
        },
        reset: function () {
            if(this.time!=0)
                this.time = DownTimer._now();
        },
        changeLength: function (hours, minutes, seconds, milliseconds) {
            this.length = ((((((hours != undefined ? hours : 0) * 60) +
                                (minutes != undefined ? minutes : 0)) * 60) +
                                (seconds != undefined ? seconds : 0)) * 1000) +
                                (milliseconds != undefined ? milliseconds : 0);
        }
    }, {
        _now: function () { return (new Date()).getTime(); },
        MillisecondsToTimeString: function (s) {
            var ms = s % 1000;
            s = (s - ms) / 1000;
            var secs = s % 60;
            s = (s - secs) / 60;
            var mins = s % 60;
            var hrs = (s - mins) / 60;
            return (hrs < 10 ? '0' : '') + hrs + ':' + (mins < 10 ? '0' : '') + mins + ':' + (secs < 10 ? '0' : '') + secs;
        }
    });

    WinJS.Namespace.define("timers", {
        DownTimer:DownTimer
    });
})()